package com.manager.core.module.manager.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.manager.commons.annotation.Module;
import com.manager.commons.base.DataGrid;
import com.manager.core.web.json.AjaxJson;
import com.manager.core.module.manager.entity.PlatformArea;
import com.manager.core.module.manager.service.PlatformAreaService;
import com.manager.gen.web.controller.AdminController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * PlatformAreaController
 * 基础配置-区域
 *
 * @author manager
 * @version 1.0
 */
@Controller()
@Module(parent = "basic-manager", title = "城市/区域", code = "/platform/area")
public class PlatformAreaController extends AdminController<PlatformArea> {
    private final String baseUrl = "/platform/area";
    @Autowired
    private PlatformAreaService _service;

    @RequestMapping(baseUrl + "/import")
    public AjaxJson importJson() {
        return result(_service.importJson(), null);
    }

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(baseUrl + "/index")
    public ModelAndView index() {
        return getModelAndView(baseUrl + "/area-index");
    }

    /**
     * 新增
     *
     * @return
     */
    @GetMapping(baseUrl + "/add")
    public ModelAndView add() {
        return getModelAndView(baseUrl + "/area-edit", getModel());
    }

    /**
     * 新增
     *
     * @return
     */
    @RequestMapping(baseUrl + "/edit")
    public ModelAndView edit() {
        return getModelAndView(baseUrl + "/area-edit", _service.findMap(getParams()));
    }

    /**
     * 删除数据
     *
     * @return
     */
    @RequestMapping(value = baseUrl + "/delete", method = RequestMethod.POST)
    public @ResponseBody AjaxJson delete() {
        return _service.delete(getParams());
    }


    /**
     * 分页
     *
     * @return
     */
    @RequestMapping(value = baseUrl + "/list")
    public @ResponseBody DataGrid page() {
        return _service.queryPage(getParams());
    }

    /**
     * 保存
     *
     * @returna
     */
    @PostMapping(value = baseUrl + "/save")
    public @ResponseBody AjaxJson save() {
        try {
            return _service.save(getParams());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return new AjaxJson("异常错误，请稍后再试");
    }

    /**
     * 改变数据状态
     *
     * @return
     */
    @PostMapping(value = baseUrl + "/status")
    public @ResponseBody AjaxJson status() {
        return _service.status(getParams());
    }

    /**
     * @return
     */
    @RequestMapping(value = baseUrl + "/tree", method = RequestMethod.POST)
    public @ResponseBody JSONArray tree() {
        return JSON.parseArray(JSONArray.toJSONString(_service.queryTreeList()));
    }

    /**
     * @return
     */
    @RequestMapping(value = baseUrl + "/select/{_id}", method = RequestMethod.POST)
    public @ResponseBody AjaxJson select(@PathVariable("_id") String _id) {
        return success(_service.querySelect(_id));
    }

    @Override
    public PlatformArea getModel() {
        return new PlatformArea();
    }

}
